#encoding=utf8
'''
Created on May 12, 2015

@author: jingyang <jingyang@smalltreemedia.com>
'''
from app_info.models import UserGroup, StudioCloud, ProductionTeam, AppVersion
from utils import JsonResponse


def filter_studio_clouds(req):
    try:
        division = UserGroup.objects.get(id=req.REQUEST.get('id'))
        clouds = [{'id': c.pk, 'name': '%s %s' % (c.cloud_id, c.get_parent_brand())}\
                  for c in division.clouds.all()]
    except Exception, e:
        print e
        clouds = []
    return JsonResponse({'res': clouds})


def filter_studios(req):
    try:
        cloud = StudioCloud.objects.get(id=req.REQUEST.get('id'))
        studios = [{'id': s.pk, 'name': s.__unicode__()}\
                   for s in cloud.studio_set.all()]
    except Exception, e:
        print e
        studios = []
    return JsonResponse({'res': studios})


def filter_team(req):
    try:
        division = UserGroup.objects.get(id=req.REQUEST.get('id'))
        team = [{'id': t.pk, 'name': t.name} for t in division.productionteam_set.all()]
    except Exception, e:
        print e
        team = []
    return JsonResponse({'res': team})


def filter_pjms(req):
    try:
        team = ProductionTeam.objects.get(id=req.REQUEST.get('id'))
        pjms = [{'id': u.pk, 'name': u.username}\
                for u in team.user.all()]
    except Exception, e:
        print e
        pjms = []
    return JsonResponse({'res': pjms})


def filter_pjm_studio(req):
    try:
        team = ProductionTeam.objects.get(id=req.REQUEST.get('id'))
        studios = [{'id': s.pk, 'name': s.__unicode__()}\
                   for s in team.studios.all()]
    except Exception, e:
        print e
        studios = []
    return JsonResponse({'res': studios})


def filter_tasks(req):
    try:
        tasks = [{'id': t.pk, 'name': t.__unicode__()}\
            for t in AppVersion.objects.filter(owner=int(req.REQUEST.get('id')))]
    except Exception, e:
        print e
        tasks = []
    return JsonResponse({'res': tasks})
